GraphQL Union Type
「指定された複数の型のうち、いずれかの型」を表現する抽象的な型
例えば type StringOrInt = String | Int というスキーマは「String または Int のうち、いずれかの型」という意味の StringOrInt union 型を定義する
union 型データをクエリで取得するときには、それぞれの具象型ごとに conditional fragment 構文で分岐してクエリを記述する必要がある
code: schema example
type Entry {
id: ID!
title: String!
content: String!
}
type Comment {
id: ID!
content: String!
}
union SearchResult = Entry | Comment
type Query {
search(q: String!): SearchResult!!
}
code: query example
query {
search(q: "foo") {
__typename # 全ての型にデフォルトで提供されるメタデータ
... on Entry {
id
title
content
}
... on Comment {
id
content
}
}
}
#GraphQL